package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.FormaPagoDAO;
import com.bit.cfdi.web.domain.FormaPago;

@Repository
public class FormaPagoDAOImpl extends HibernateDaoSupport implements FormaPagoDAO {

	@Autowired public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@Override public List<FormaPago> getAll() {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override 
	@Transactional 
	public FormaPago getByDescription(String description) 
			throws DataAccessException {
		
		String query = "from FormaPago as f where f.descripcion = ?";
		List<FormaPago> formaPago = getHibernateTemplate().find(query, description);		
		return formaPago != null && !formaPago.isEmpty() ? formaPago.get(0) : null;
	}

	@Override	
	@Transactional public void save(FormaPago formaPago) 
			throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(formaPago);
	}

}